﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Diagnostics;
using System.Xml.Linq;

namespace Obligatorio2013.Persistencia.Daos
{
    public abstract class PersistentDAO
    {

        #region attributes
        // Aca se definen donde estan guardados los archivos. 
        private static string localPath = AppDomain.CurrentDomain.BaseDirectory + "App_Data\\";

        protected static string path
        {
            get { return localPath; }
            set { localPath = value; }
        }

        #endregion

        #region contructors
        public PersistentDAO()
        {
        }

        #endregion

        #region common methods
        protected static bool FileExist(string fileName)
        {
            return System.IO.File.Exists(path + fileName);
        }
        
        protected static XmlDocument LoadXmlFile(string path, string fileName)
        {
            if (!FileExist(fileName))
                return null;
            XmlDocument newDoc = new XmlDocument();
            newDoc.PreserveWhitespace = false;
            newDoc.Load(path + fileName);
            return newDoc;

        }

        protected static void createFileXml(string path, string fileName, string principalNode)
        {

            XmlTextWriter writer;
            using (writer = new XmlTextWriter(path + fileName, System.Text.Encoding.UTF8))
            {
                writer.Formatting = Formatting.Indented;
                writer.WriteStartDocument(true);
                writer.WriteStartElement(principalNode);
                writer.WriteEndElement();
                writer.WriteEndDocument();
                writer.Flush(); // Para que libere memoria y tire a disco
                writer.Close();
            }
        }

        protected static XmlNode addElement(XmlDocument doc, XmlNode parent, string nodeName)
        {

            XmlNode childNode = doc.CreateElement(nodeName);
            parent.AppendChild(childNode);
            Debug.Write(parent.Name); // esto es log
            return childNode;
        }


        protected static XmlNode addElement(XmlDocument doc, XmlNode parent, string
    nodeName, string value)
        {

            XmlNode childNode = addElement(doc, parent, nodeName);
            if (value != null)
                childNode.InnerText = value; // esto agrega att del padre.

            return childNode;
        }

        protected static XmlAttribute addAttribute(XmlDocument doc, XmlNode node,
        string name, string value)
        {
            XmlAttribute attr = doc.CreateAttribute(name);
            attr.Value = value;
            node.Attributes.Append(attr);
            return attr;
        }

        protected static XmlNode getElementByID(XmlDocument doc, string nodeName, string key, string keyValue)
        {
            XmlNode element;//=documento.SelectSingleNode(@nombreNodo);
            // Aca se esta utilizando xPath.
            string keySearch = nodeName + "[@" + key + "='" + keyValue + "']";
            element = doc.DocumentElement.SelectSingleNode(keySearch);
            return element;
        }


        // Metodos Nico para obtener elementos y atributos del xml.
        protected static string GetElementValue(string elementName, string attributeName, XElement element)
        {
            XElement xElement = element.Element(elementName);

            string value = string.Empty;

            if (xElement != null)
            {
                value = xElement.Value;
            }

            return value;
        }
        protected static string GetAttributeValue(string attributeName, XElement element)
        {
            XAttribute xAttribute = element.Attribute(attributeName);

            string value = string.Empty;
            if (xAttribute != null)
            {
                value = xAttribute.Value;
            }

            return value;
        }

        #endregion

        #region abstract methods for CRUD

        public abstract bool Save(Object obj);
        public abstract List<Object> getAll();
        public abstract bool Update(Object objId);
        public abstract bool Delete(Object objId);

        #endregion
    }
}
